//
//  LGCoompostType.swift
//  我的微博
//
//  Created by ming on 16/10/19.
//  Copyright © 2016年 ming. All rights reserved.
//

import UIKit

class LGCoompostType: UIView {

    @IBOutlet weak var coposeType: UIScrollView!
    /// 按钮数据数组
    private let buttonsInfo = [["imageName": "tabbar_compose_idea", "title": "文字", "clsName": "WBComposeViewController"],
                               ["imageName": "tabbar_compose_photo", "title": "照片/视频"],
                               ["imageName": "tabbar_compose_weibo", "title": "长微博"],
                               ["imageName": "tabbar_compose_lbs", "title": "签到"],
                               ["imageName": "tabbar_compose_review", "title": "点评"],
                               ["imageName": "tabbar_compose_more", "title": "更多", "actionName": "clickMore"],
                               ["imageName": "tabbar_compose_friend", "title": "好友圈"],
                               ["imageName": "tabbar_compose_wbcamera", "title": "微博相机"],
                               ["imageName": "tabbar_compose_music", "title": "音乐"],
                               ["imageName": "tabbar_compose_shooting", "title": "拍摄"]
    ]

    
    class func compostTypeView()->(LGCoompostType){
    let nib = UINib(nibName: "LGCoompostType", bundle: nil)
       
      
        let cView = nib.instantiate(withOwner: nil, options: nil)[0]
       
          let v = cView as! (LGCoompostType)
        v.frame = UIScreen.main.bounds
         //解决出现600问题
          v.setupUI()
        return v
        
    }
    
    //显示界面
    func show(){
        
        let coposeView = LGCoompostType.compostTypeView()
        coposeView.frame = UIScreen.main.bounds
        
        UIApplication.shared.keyWindow?.rootViewController?.view.addSubview(coposeView)
        
    }
    
    func setupUI(){
         //强制跟新frame。
        layoutIfNeeded()
        let size = coposeType.bounds
        for i in 0..<2 {
            let v = UIView(frame: size)
            v.frame = coposeType.bounds.offsetBy(dx: CGFloat(i) * size.width, dy: 0)
            
            addButton(v: v, idx: i * 6)
            coposeType.addSubview(v)
        }
        coposeType.contentSize = CGSize(width: 2 * UIScreen.main.bounds.width, height: 0)
        coposeType.showsHorizontalScrollIndicator = false
        coposeType.showsVerticalScrollIndicator = false

        coposeType.isScrollEnabled = false
        
        
        
       
    }
    
    //向v中添加按钮，按钮的数组是从ide开始的
    func addButton(v:UIView,idx:Int){
        //添加按钮
        let count = 6
        
        for i in idx..<(idx + count) {
            
            if i >= buttonsInfo.count{
                break
            }
            
        let dict = buttonsInfo[i]
     guard   let imageName = dict["imageName"],
        let title = dict["title"] else{
                continue
            }
            //创建按钮
          let btn = LGCoposeTypeButn.composeTypeButton(imageName: imageName, title: title)
         
            //把按钮添加到视图中
        v.addSubview(btn)
           //添加单击
            if let clickMore = dict["actionName"] {
                
                btn.addTarget(self, action: Selector(clickMore), for: .touchUpInside)
            }
            
        
        }
        //计算frame
    
        let btnSize = CGSize(width: 100, height: 100)
        let margin = (v.bounds.width - 3 * btnSize.width) / 4
        
        for (i, btn) in v.subviews.enumerated() {
            
            let y: CGFloat = (i > 2) ? (v.bounds.height - btnSize.height) : 0
            let col = i % 3
            let x = CGFloat(col + 1) * margin + CGFloat(col) * btnSize.width
            
            btn.frame = CGRect(x: x, y: y, width: btnSize.width, height: btnSize.height)
            print(btn.frame)
        }
        
        
        
        
    }
    func clickMore(){
        
        print("点击我了");
    }
    
    
    @IBAction func close(_ sender: AnyObject) {
        
        removeFromSuperview()
    }
}
